﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class LocationList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F2152;
        protected GridView gvLocation;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlInputHidden hiddenFlag;
        protected Label lblLocation;
        protected Label lblMsg;
        protected LinkButton lnkbtnImportData;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnImportData;
        protected UpdatePanel up1;

        private void BindData2LocationList(LocationQueryEntity LocationQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(LocationQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(LocationQuery, "LOCATION");
            base.BindData2GridView(this.gvLocation, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLocation, "LOC");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(selectedID, "LOCATION") + base.GetGlobalMsgResource("C006");
                    LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
                    this.BindData2LocationList(locationQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvLocation, "LOC");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("Location.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
            locationQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = locationQuery.CurrentPage;
            locationQuery.CurrentPage = "0";
            try
            {
                using (DataSet ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(locationQuery, "LOCATION"))
                {
                    base.Export2Excel("Location.xls", "2152", this.gvLocation, ds);
                }
            }
            finally
            {
                locationQuery.IsGetAll = false;
                locationQuery.CurrentPage = currentPageIndex;
                this.SetLocationQueryEntity(locationQuery);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
            locationQuery.CurrentPage = "0";
            this.BindData2LocationList(locationQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
            locationQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2LocationList(locationQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
            locationQuery.Loc = "";
            locationQuery.ZoneID = "";
            locationQuery.IsActive = "";
            this.BindData2LocationList(locationQuery);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer("LocationQuery.aspx");
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
            if (int.Parse(locationQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                locationQuery.CurrentPage = (int.Parse(locationQuery.CurrentPage) + 1).ToString();
                this.BindData2LocationList(locationQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
            if (int.Parse(locationQuery.CurrentPage) > 0)
            {
                locationQuery.CurrentPage = (int.Parse(locationQuery.CurrentPage) - 1).ToString();
                this.BindData2LocationList(locationQuery);
            }
        }

        private LocationQueryEntity GetLocationQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as LocationQueryEntity);
            }
            return (base.PageQueryEntitySession as LocationQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                LocationQueryEntity locationQuery = this.GetLocationQueryEntity();
                locationQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2LocationList(locationQuery);
            }
        }

        protected void gvLocation_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2152", this.gvLocation);
        }

        protected void gvLocation_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvLocation_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvLocation.DataKeys[e.NewSelectedIndex]["LOC"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + strValue + "';  window.close();</script>", false);
            }
        }

        protected void gvLocation_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                LocationQueryEntity locQuery = this.GetLocationQueryEntity();
                locQuery.SortField = e.SortExpression;
                if (locQuery.SortDirection == "ASC")
                {
                    locQuery.SortDirection = "DESC";
                }
                else if (locQuery.SortDirection == "DESC")
                {
                    locQuery.SortDirection = "ASC";
                }
                this.BindData2LocationList(locQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            LocationQueryEntity locationQuery = new LocationQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    locationQuery.Loc = this.PreviousPage.LocID;
                    locationQuery.ZoneID = this.PreviousPage.ZoneID;
                    locationQuery.IsActive = this.PreviousPage.IsActive;
                }
                else
                {
                    locationQuery.Loc = "";
                    locationQuery.ZoneID = "";
                    locationQuery.IsActive = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            locationQuery.IsGetAll = false;
            locationQuery.CurrentPage = "0";
            locationQuery.PageSize = this.gvLocation.PageSize.ToString();
            locationQuery.SortField = "loc.ROWID";
            locationQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvLocation.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvLocation.Columns[1].Visible = false;
                this.gvLocation.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnAction.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
            }
            else if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetLocationQueryEntity(locationQuery);
            this.BindData2LocationList(locationQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLocationQueryEntity(LocationQueryEntity uomQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = uomQuery;
            }
            else
            {
                base.PageQueryEntitySession = uomQuery;
            }
        }

        public LocationQuery PreviousPage
        {
            get
            {
                return (LocationQuery) base.PreviousPage;
            }
        }
    }
}

